//
//  Details.m
//
//  Created by TuanNT on 5/25/14
//  Copyright (c) 2014 Fsoft. All rights reserved.
//

#import "Account.h"


NSString *const kDetailsDateOfBirth = @"dateOfBirth";
NSString *const kDetailsState = @"state";
NSString *const kDetailsFollowersCount = @"followers_count";
NSString *const kDetailsGender = @"gender";
NSString *const kDetailsEmail = @"email";
NSString *const kDetailsPostcode = @"postcode";
NSString *const kDetailsLastname = @"lastname";
NSString *const kDetailsFollowingCount = @"following_count";
NSString *const kDetailsCompany = @"company";
NSString *const kDetailsPrimaryRole = @"PrimaryRole";
NSString *const kDetailsFirstname = @"firstname";
NSString *const kDetailsFax = @"fax";
NSString *const kDetailsImage = @"image";
NSString *const kDetailsCity = @"city";
NSString *const kDetailsCustomerId = @"customer_id";
NSString *const kDetailsWebsite = @"website";
NSString *const kDetailsStreetAddress = @"street_address";
NSString *const kDetailsTelephone = @"telephone";
NSString *const kDetailsCountry = @"country";


@interface Account ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation Account

@synthesize dateOfBirth = _dateOfBirth;
@synthesize state = _state;
@synthesize followersCount = _followersCount;
@synthesize gender = _gender;
@synthesize email = _email;
@synthesize postcode = _postcode;
@synthesize lastname = _lastname;
@synthesize followingCount = _followingCount;
@synthesize company = _company;
@synthesize primaryRole = _primaryRole;
@synthesize firstname = _firstname;
@synthesize fax = _fax;
@synthesize image = _image;
@synthesize city = _city;
@synthesize customerId = _customerId;
@synthesize website = _website;
@synthesize streetAddress = _streetAddress;
@synthesize telephone = _telephone;
@synthesize country = _country;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
	return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
	self = [super init];

	// This check serves to make sure that a non-NSDictionary object
	// passed into the model class doesn't break the parsing.
	if (self && [dict isKindOfClass:[NSDictionary class]]) {
		self.dateOfBirth = [self objectOrNilForKey:kDetailsDateOfBirth fromDictionary:dict];
		self.state = [[self objectOrNilForKey:kDetailsState fromDictionary:dict] boolValue];
		self.followersCount = [self objectOrNilForKey:kDetailsFollowersCount fromDictionary:dict];
		self.gender = [self objectOrNilForKey:kDetailsGender fromDictionary:dict];
		self.email = [self objectOrNilForKey:kDetailsEmail fromDictionary:dict];
		self.postcode = [self objectOrNilForKey:kDetailsPostcode fromDictionary:dict];
		self.lastname = [self objectOrNilForKey:kDetailsLastname fromDictionary:dict];
		self.followingCount = [self objectOrNilForKey:kDetailsFollowingCount fromDictionary:dict];
		self.company = [self objectOrNilForKey:kDetailsCompany fromDictionary:dict];
		self.primaryRole = [self objectOrNilForKey:kDetailsPrimaryRole fromDictionary:dict];
		self.firstname = [self objectOrNilForKey:kDetailsFirstname fromDictionary:dict];
		self.fax = [self objectOrNilForKey:kDetailsFax fromDictionary:dict];
		self.image = [self objectOrNilForKey:kDetailsImage fromDictionary:dict];
		self.city = [self objectOrNilForKey:kDetailsCity fromDictionary:dict];
		self.customerId = [self objectOrNilForKey:kDetailsCustomerId fromDictionary:dict];
		self.website = [self objectOrNilForKey:kDetailsWebsite fromDictionary:dict];
		self.streetAddress = [self objectOrNilForKey:kDetailsStreetAddress fromDictionary:dict];
		self.telephone = [self objectOrNilForKey:kDetailsTelephone fromDictionary:dict];
		self.country = [self objectOrNilForKey:kDetailsCountry fromDictionary:dict];
	}

	return self;
}

- (NSDictionary *)dictionaryRepresentation {
	NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
	[mutableDict setValue:self.dateOfBirth forKey:kDetailsDateOfBirth];
	[mutableDict setValue:[NSNumber numberWithBool:self.state] forKey:kDetailsState];
	[mutableDict setValue:self.followersCount forKey:kDetailsFollowersCount];
	[mutableDict setValue:self.gender forKey:kDetailsGender];
	[mutableDict setValue:self.email forKey:kDetailsEmail];
	[mutableDict setValue:self.postcode forKey:kDetailsPostcode];
	[mutableDict setValue:self.lastname forKey:kDetailsLastname];
	[mutableDict setValue:self.followingCount forKey:kDetailsFollowingCount];
	[mutableDict setValue:self.company forKey:kDetailsCompany];
	[mutableDict setValue:self.primaryRole forKey:kDetailsPrimaryRole];
	[mutableDict setValue:self.firstname forKey:kDetailsFirstname];
	[mutableDict setValue:self.fax forKey:kDetailsFax];
	[mutableDict setValue:self.image forKey:kDetailsImage];
	[mutableDict setValue:self.city forKey:kDetailsCity];
	[mutableDict setValue:self.customerId forKey:kDetailsCustomerId];
	[mutableDict setValue:self.website forKey:kDetailsWebsite];
	[mutableDict setValue:self.streetAddress forKey:kDetailsStreetAddress];
	[mutableDict setValue:self.telephone forKey:kDetailsTelephone];
	[mutableDict setValue:self.country forKey:kDetailsCountry];

	return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description {
	return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
	id object = [dict objectForKey:aKey];
	return [object isEqual:[NSNull null]] ? nil : object;
}

#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
	self = [super init];

	self.dateOfBirth = [aDecoder decodeObjectForKey:kDetailsDateOfBirth];
	self.state = [aDecoder decodeBoolForKey:kDetailsState];
	self.followersCount = [aDecoder decodeObjectForKey:kDetailsFollowersCount];
	self.gender = [aDecoder decodeObjectForKey:kDetailsGender];
	self.email = [aDecoder decodeObjectForKey:kDetailsEmail];
	self.postcode = [aDecoder decodeObjectForKey:kDetailsPostcode];
	self.lastname = [aDecoder decodeObjectForKey:kDetailsLastname];
	self.followingCount = [aDecoder decodeObjectForKey:kDetailsFollowingCount];
	self.company = [aDecoder decodeObjectForKey:kDetailsCompany];
	self.primaryRole = [aDecoder decodeObjectForKey:kDetailsPrimaryRole];
	self.firstname = [aDecoder decodeObjectForKey:kDetailsFirstname];
	self.fax = [aDecoder decodeObjectForKey:kDetailsFax];
	self.image = [aDecoder decodeObjectForKey:kDetailsImage];
	self.city = [aDecoder decodeObjectForKey:kDetailsCity];
	self.customerId = [aDecoder decodeObjectForKey:kDetailsCustomerId];
	self.website = [aDecoder decodeObjectForKey:kDetailsWebsite];
	self.streetAddress = [aDecoder decodeObjectForKey:kDetailsStreetAddress];
	self.telephone = [aDecoder decodeObjectForKey:kDetailsTelephone];
	self.country = [aDecoder decodeObjectForKey:kDetailsCountry];
	return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
	[aCoder encodeObject:_dateOfBirth forKey:kDetailsDateOfBirth];
	[aCoder encodeBool:_state forKey:kDetailsState];
	[aCoder encodeObject:_followersCount forKey:kDetailsFollowersCount];
	[aCoder encodeObject:_gender forKey:kDetailsGender];
	[aCoder encodeObject:_email forKey:kDetailsEmail];
	[aCoder encodeObject:_postcode forKey:kDetailsPostcode];
	[aCoder encodeObject:_lastname forKey:kDetailsLastname];
	[aCoder encodeObject:_followingCount forKey:kDetailsFollowingCount];
	[aCoder encodeObject:_company forKey:kDetailsCompany];
	[aCoder encodeObject:_primaryRole forKey:kDetailsPrimaryRole];
	[aCoder encodeObject:_firstname forKey:kDetailsFirstname];
	[aCoder encodeObject:_fax forKey:kDetailsFax];
	[aCoder encodeObject:_image forKey:kDetailsImage];
	[aCoder encodeObject:_city forKey:kDetailsCity];
	[aCoder encodeObject:_customerId forKey:kDetailsCustomerId];
	[aCoder encodeObject:_website forKey:kDetailsWebsite];
	[aCoder encodeObject:_streetAddress forKey:kDetailsStreetAddress];
	[aCoder encodeObject:_telephone forKey:kDetailsTelephone];
	[aCoder encodeObject:_country forKey:kDetailsCountry];
}

- (id)copyWithZone:(NSZone *)zone {
	Account *copy = [[Account alloc] init];

	if (copy) {
		copy.dateOfBirth = [self.dateOfBirth copyWithZone:zone];
		copy.state = self.state;
		copy.followersCount = [self.followersCount copyWithZone:zone];
		copy.gender = [self.gender copyWithZone:zone];
		copy.email = [self.email copyWithZone:zone];
		copy.postcode = [self.postcode copyWithZone:zone];
		copy.lastname = [self.lastname copyWithZone:zone];
		copy.followingCount = [self.followingCount copyWithZone:zone];
		copy.company = [self.company copyWithZone:zone];
		copy.primaryRole = [self.primaryRole copyWithZone:zone];
		copy.firstname = [self.firstname copyWithZone:zone];
		copy.fax = [self.fax copyWithZone:zone];
		copy.image = [self.image copyWithZone:zone];
		copy.city = [self.city copyWithZone:zone];
		copy.customerId = [self.customerId copyWithZone:zone];
		copy.website = [self.website copyWithZone:zone];
		copy.streetAddress = [self.streetAddress copyWithZone:zone];
		copy.telephone = [self.telephone copyWithZone:zone];
		copy.country = self.country;
	}

	return copy;
}

@end
